package com.project.daojishi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.R.integer;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


public class SqliteDBUtils {

    public static final String DB_NAME = "sqlite_dbname";

    public static final int VERSION = 1;
 
    private static SqliteDBUtils sqliteDB;
 
    private SQLiteDatabase db;
 
    private SqliteDBUtils(Context context) {
        OpenHelper dbHelper = new OpenHelper(context, DB_NAME, null, VERSION);
        db = dbHelper.getWritableDatabase();
    }
 

    public synchronized static SqliteDBUtils getInstance(Context context) {
        if (sqliteDB == null) {
            sqliteDB = new SqliteDBUtils(context);
        }
        return sqliteDB;
    }
 
    
    public void delete(Context context,int id) {
        OpenHelper dbHelper = new OpenHelper(context, DB_NAME, null, VERSION);
    	 db = dbHelper.getReadableDatabase();
    	 db.delete("Diary", "id=?", new String[] { id+"" });
	}
    
    public void change(Context context,Diary diary) {
    	   OpenHelper dbHelper = new OpenHelper(context, DB_NAME, null, VERSION);
      	 db = dbHelper.getWritableDatabase();
      	ContentValues values = new ContentValues();
      	values.put("id", diary.getId());
        values.put("title", diary.getTitle());
        values.put("type", diary.getType());
        values.put("chongfu", diary.getChongfu());
        values.put("time", diary.getTime());
      	 db.update("Diary", values, "id=?", new String[]{diary.getId()+""});
	}
    public void   save(Diary user) {

        try {
            db.execSQL("insert into Diary(title,time,chongfu,type) values(?,?,?,?) ", new String[]{
                    user.getTitle().toString(),
                    user.getTime().toString(),
                    user.getChongfu(),
                    user.getType()
            });
        } catch (Exception e) {
            Log.d("����", e.getMessage().toString());
        }
    }
 

    @SuppressLint("Range")
    public List<Diary> load() {
        List<Diary> list = new ArrayList<Diary>();
        Cursor cursor = db
                .query("Diary", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                Diary user = new Diary();
                user.setId(cursor.getInt(cursor.getColumnIndex("id")));
                user.setType(cursor.getString(cursor .getColumnIndex("type")));
                user.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                user.setTime(cursor.getString(cursor.getColumnIndex("time")));
                user.setChongfu(cursor.getString(cursor.getColumnIndex("chongfu")));

                list.add(user);
            } while (cursor.moveToNext());
        }
        return list;
    }


}